package com.nsqt.biz.controller;

import com.nsqt.biz.domain.CustomerDO;
import com.nsqt.biz.service.CustomerService;
import com.nsqt.common.utils.PageUtils;
import com.nsqt.common.utils.Query;
import com.nsqt.common.utils.R;
import com.nsqt.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping("/biz/myCustomer")
public class MyCustomerController {
    @Autowired
    private CustomerService customerService;

    @GetMapping()
    @RequiresPermissions("biz:myCustomer:myCustomer")
    String Customer() {
        return "biz/mycustomer/mycustomer";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("biz:myCustomer:myCustomer")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        Long userId = ShiroUtils.getUserId();
        params.put("saler", userId);
        //查询列表数据
        Query query = new Query(params);
        List<CustomerDO> customerList = customerService.list(query);
        int total = customerService.count(query);
        PageUtils pageUtils = new PageUtils(customerList, total);
        return pageUtils;
    }

    /**
     * 新增
     */
    @GetMapping("/add")
    @RequiresPermissions("biz:myCustomer:add")
    String add() {
        return "biz/myCustomer/add";
    }

    /**
     * 编辑
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("biz:myCustomer:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        CustomerDO customer = customerService.get(id);
        model.addAttribute("customer", customer);
        return "biz/customer/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("biz:myCustomer:add")
    public R save(CustomerDO customer) {
        if (customerService.save(customer) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("biz:myCustomer:edit")
    public R update(CustomerDO customer) {
        customerService.update(customer);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("biz:myCustomer:remove")
    public R remove(Long id) {
        if (customerService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("biz:myCustomer:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        customerService.batchRemove(ids);
        return R.ok();
    }

    /**
     * 认领
     */
    @PostMapping("/turnback")
    @ResponseBody
    @RequiresPermissions("biz:myCustomer:turnback")
    public R turnback(Long id) {
        CustomerDO customerDO = customerService.get(id);
        customerDO.setSaler(1);
        customerDO.setStatus(0);
        if (customerService.update(customerDO) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量领取
     */
    @PostMapping("/batchTurnback")
    @ResponseBody
    @RequiresPermissions("biz:myCustomer:batchTurnback")
    public R batchTurnback(@RequestParam("ids[]") Long[] ids) {

        for (Long id : ids
        ) {
            try {
                CustomerDO customerDO = customerService.get(id);
                customerDO.setSaler(1);
                customerDO.setStatus(0);
                customerService.update(customerDO);
            } catch (Exception e) {
                continue;
            }
        }

        return R.ok();
    }
}
